﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;
using Entity;

namespace StudentAttendanceChecking.Forms
{
    public partial class frmViewBranch : Form
    {
        public frmViewBranch()
        {
            InitializeComponent();
        }
        DataSet ds = new DataSet();
        int i;
        private void viewBranch()
        {
            SqlConnection conn = ConnectData.getConnection();
            conn.Open();
            SqlCommand cmd = new SqlCommand("Get_Branches", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            ds.Clear();
            da.Fill(ds);
            
            grvViewBranch.DataSource = ds.Tables[0];
        }
        private void frmViewBranch_Load(object sender, EventArgs e)
        {
            viewBranch();
            
        }

        private void grvViewBranch_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEdit.Enabled = true;
            btnDelete.Enabled = true;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int i = 0;
            for (i = 0; i < grvViewBranch.Rows.Count - 1; i++)
            {
                frmViewBranch.sql("Update Branch Set BranchName=N'" + grvViewBranch.Rows[i].Cells[1].Value + "' Where BranchID='" + grvViewBranch.Rows[i].Cells[0].Value + "'");

            }
            MessageBox.Show("Update Complete", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
            viewBranch();
        }

        public static void sql(string sql)
        {
            SqlConnection conn = ConnectData.getConnection();
            SqlDataAdapter myData = new SqlDataAdapter(sql, conn);
            DataSet myDataSet = new DataSet();
            myData.Fill(myDataSet);
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            int i = grvViewBranch.CurrentRow.Index;
            DialogResult rs = MessageBox.Show(this, "Are you sure delete", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rs == DialogResult.Yes)
            {
                frmViewBranch.sql("Delete from Branch where BranchID='" + grvViewBranch.Rows[i].Cells[0].Value + "'");
            }
            else {
              
            }
          

        }
    }
}
